import $utils from '@/plugins/utils';
import store from '@/store';
import { whitePages } from '@/plugins/http/whitePages';
const { UtilTools, getCurPage, aesEncrypt, aesDecrypt } = $utils;
import { appkey, secret, isCrypto } from '@/assets/config.js';
export default config => {
	return new Promise((resolve, reject) => {
		const page = getCurPage().route;
		config.header = UtilTools.extend({
			'content-type': 'application/json; charset=UTF-8',
			secret,
			appkey
		}, config.header);
		const contentType = config.header['content-type'] || config.header['Content-Type'];
		if ((contentType === undefined || contentType.indexOf('application/json') > -1) && Number(
			isCrypto)) {
			config.header.Crypto = isCrypto;
			config.data = { 'data': aesEncrypt(config.data) };
		}
		const { userInfo = {} } = store.state;
		const { token = 'njpsmini' } = userInfo
		if (whitePages.includes(page)) {
			resolve(config);
		} else if (token) {
			resolve(config);
		} else {
			reject('登录失效，请重新登录');
			if (page !== 'pages/login/index') {
				setTimeout(() => {
					store.commit('commitClear');
					uni.reLaunch({
						'url': '/pages/login/index'
					});
				}, 2001);
			}
		}
	});
};